package com.woniuxy.microsysserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.bean.ResultBean;
import com.woniuxy.microsysserver.entity.CouponEntity;
import com.woniuxy.microsysserver.entity.MemberCouponEntity;
import com.woniuxy.microsysserver.entity.MyPage;
import com.woniuxy.microsysserver.service.CouponService;
import com.woniuxy.microsysserver.service.MemberCouponService;
import com.woniuxy.microsysserver.service.MemberService;
import com.woniuxy.microsysserver.uitl.Constant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Auther: wuson  <br/>
 * Date: 2021/4/15:20:04  <br/>
 * Description:
 */
@RefreshScope
@Api("优惠券管理")
@Slf4j
@RequestMapping("/sys/coupon")
@RestController
public class CouponController {
    @Autowired(required = false)
    private CouponService couponConService;
    @Autowired(required = false)
    private MemberCouponService memberCouponService;

    @ApiOperation("新增优惠券")
    @PostMapping(value = "/addOne",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean addOne(@RequestBody CouponEntity couponEntity){
       int state = couponConService.addOne(couponEntity);
        return ResultBean.builder().code(200).data(Constant.SUCCESSFUL).build();
    }
    @ApiOperation("删除优惠券")
    @GetMapping(value = "/deleteOne",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean deleteOne(int couponId){
        int state = couponConService.deleteOne(couponId);
        return ResultBean.builder().code(200).data(Constant.SUCCESSFUL).build();
    }
    @ApiOperation("修改优惠券")
    @PostMapping(value = "/updateOne",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean updateOne(@RequestBody CouponEntity couponEntity){
        int state = couponConService.updateOne(couponEntity);
        return ResultBean.builder().code(200).data(Constant.SUCCESSFUL).build();
    }

    @ApiOperation("分页查询用户未领取优惠券")
    @PostMapping(value = "/selectAll",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean selectAll(@RequestBody MyPage myPage){
        //开始分页
        PageHelper.startPage(myPage.getPageNum(), myPage.getPageSize());
        //查询所有结果
        List<CouponEntity> couponEntitys = couponConService.selectAll();
        //查询用户已领优惠券
        List<MemberCouponEntity> memberCouponEntities = memberCouponService.getcouponByUsername(myPage.getUsername());
        List<Integer> couponids = new ArrayList<>();
        //将已领取优惠券id装入couponids
        for (MemberCouponEntity memberCouponEntity : memberCouponEntities) {
            couponids.add(memberCouponEntity.getCouponId());
        }
        List<CouponEntity> newCouponEntitys = new ArrayList<>();
//        移除couponEntitys中包含id在couponids的对象
        for (CouponEntity couponEntity : couponEntitys) {
            if(!couponids.contains(couponEntity.getId())){
//                couponEntitys.remove(couponEntity);
                newCouponEntitys.add(couponEntity);
            }
        }
//        Iterator<CouponEntity> couponEntityIterator = couponEntitys.iterator();
//        while(couponEntityIterator.hasNext()) {
//            CouponEntity next = couponEntityIterator.next();
//            if(couponids.contains(next.getId())){
//                couponEntityIterator.remove();
//            }
//        }
        //分页
        PageInfo<CouponEntity> pageInfo = new PageInfo<>(newCouponEntitys);
        return ResultBean.builder().code(200).data(pageInfo).build();
    }
}
